package com.rhb.oa.news.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

import com.rhb.exception.DuplicateException;
import com.rhb.oa.news.bean.*;
import com.rhb.oa.news.web.struts.form.*;
import com.rhb.oa.web.struts.action.OaBaseDispatchAction;

public class NewsSaveAction extends OaBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return mapping.findForward("fail");
	}
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		NewsForm oform = (NewsForm)form;
		this.getNewsBusiness().delete(oform.getNews());
		return mapping.findForward("success");
	}
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		NewsForm oform = (NewsForm)form;
		ActionMessages messages = new ActionMessages();
		try {
			this.getNewsBusiness().update(oform.getNews());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward("success");
	}
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		NewsForm oform = (NewsForm)form;
		ActionMessages messages = new ActionMessages();
		try {
			this.getNewsBusiness().create(oform.getNews());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward("success");
	}	 
}
